package sort.popSort;

/**
 * @Author: zhang
 * @Description:冒泡排序
 * @Date: Created in 15:49 2019/9/30
 */
public class Test {
    public static void main(String[] args) {
        int[] arrays = new int[]{5, 1, 7, 4, 8, 9, 2, 3, 6, 0};
        popSort(arrays);
        for (int i = 0; i < arrays.length; i++) {
            if (i != arrays.length - 1) {
                System.out.print(arrays[i] + " ");
            } else {
                System.out.print(arrays[i]);
            }
        }
    }

    /**
     * 冒泡排序
     * 每次将一个数沉到尾部
     * 两个for循环，外层控制循环次数，内层控制交换次数
     */
    public static void popSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            boolean flag = true;
            for (int j = 0; j < a.length - i; j++) {
                if (a[j] > a[j + 1]) {
                    a[j] ^= a[j + 1];
                    a[j + 1] ^= a[j];
                    a[j] ^= a[j + 1];
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }
}
